//
//  XLFormDetailCell.m
//  XZPlatformKit
//
//  Created by Basic on 2018/10/24.
//

#import "XLFormDetailCell.h"
#import <Masonry/Masonry.h>
#import <BCComConfigKit/BCComConfig.h>

NSString *const XLFormRowDescriptorTypeDetail = @"XLFormRowDescriptorTypeDetail";

#define kXLFormDetailCell_DefaultHeight    48//默认 高度

@interface XLFormDetailCell()
@property (strong, nonatomic) UILabel *titleLb;
@property (strong, nonatomic) UILabel *detailLb;
@end

@implementation XLFormDetailCell

#pragma mark - system
+(void)load {
    [XLFormViewController.cellClassesForRowDescriptorTypes setObject:[XLFormDetailCell class] forKey:XLFormRowDescriptorTypeDetail];
}

#pragma mark - getter
-(UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.font = [UIFont systemFontOfSize:15];
        _titleLb.textColor = UIColor.blackColor;
        _titleLb.numberOfLines = 0;
    }
    return _titleLb;
}
-(UILabel *)detailLb {
    if (!_detailLb) {
        _detailLb = [[UILabel alloc] init];
        _detailLb.font = [UIFont systemFontOfSize:15];
        _detailLb.textColor = UIColor.blackColor;
        _detailLb.numberOfLines = 0;
        _detailLb.textAlignment = NSTextAlignmentRight;
    }
    return _detailLb;
}


#pragma mark - XLFormDescriptorCell
- (void)configure
{
    [super configure];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // title UI
    [self.contentView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(60);
        make.centerY.mas_equalTo(self.contentView);
    }];
    // detail UI
    [self.contentView addSubview:self.detailLb];
    [self.detailLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLb.mas_right).mas_offset(20);
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView);
    }];
    // * UI
    [self.contentView addSubview:self.asteriskLab];
    [self.asteriskLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.titleLb.mas_left).mas_offset(-1);
        make.centerY.mas_equalTo(self.contentView);
    }];
}

- (void)update
{
    [super update];
    self.asteriskLab.hidden = !self.rowDescriptor.required;
    // title
    if (self.rowDescriptor.titleFont) {
        self.titleLb.font = self.rowDescriptor.titleFont;
    }
    if (self.rowDescriptor.titleColor) {
        self.titleLb.textColor = self.rowDescriptor.titleColor;
    }
    if (self.rowDescriptor.titleWidth>0) {
        [self.titleLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.rowDescriptor.titleWidth);
        }];
    }
    if (self.rowDescriptor.titleLeft>0) {
        [self.titleLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.rowDescriptor.titleLeft);
        }];
    }
    self.titleLb.textAlignment = self.rowDescriptor.titleAlignment;
    self.titleLb.text = self.rowDescriptor.title;
    [self.titleLb sizeToFit];
    //detail
    if(self.rowDescriptor.textLeft>0) {
        [self.detailLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.rowDescriptor.textLeft);
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self.contentView);
        }];
    } else {
        [self.detailLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLb.mas_right).mas_offset(20);
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    if (self.rowDescriptor.detailFont) {
        self.detailLb.font = self.rowDescriptor.detailFont;
    }
    if (self.rowDescriptor.detailColor) {
        self.detailLb.textColor = self.rowDescriptor.detailColor;
    }
    self.detailLb.textAlignment = self.rowDescriptor.detailAlignment;
    self.detailLb.text = self.rowDescriptor.displayTextValue;
    [self.detailLb sizeToFit];
}
+(CGFloat)formRowHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor {
    CGFloat height = kXLFormDetailCell_DefaultHeight;//默认48 高度
    CGFloat valueHeight = 0;
    //计算 title 高度
    CGFloat titleHeight = [NSString bc_autoHeight:rowDescriptor.title stringFont:rowDescriptor.titleFont textWidth:rowDescriptor.titleWidth];
    //计算value 高度
    if ([rowDescriptor.value isKindOfClass:[NSString class]] && ((NSString *)rowDescriptor.value).length>0) {
        valueHeight = [NSString bc_autoHeight:rowDescriptor.value stringFont:rowDescriptor.detailFont textWidth:(kBCSCREEN_WIDTH-100)];
    }
    height = MAX(titleHeight, valueHeight);
    //判断文字高度是否大于默认高度
    if (height>kXLFormDetailCell_DefaultHeight) {
        //文字高度 上下各加 8
        height = height + 16;
    } else {
        height = kXLFormDetailCell_DefaultHeight;
    }
    return height;
}

-(BOOL)showBottomSplitLine {
    return YES;
}
@end
